package com.atguigu.lease.common.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author 剑落萧瑟不自知
 * @Time 2024/1/26
 * @OverView
 */

@Configuration
public class redisTemplateConfiuration {

    @Bean
    //创建需要序列化的template
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        //序列化
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        //设置链接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //创建json序列化
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

        //设置key的序列化

        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.string());

        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setValueSerializer(serializer);

        //返回修改的对象
        return redisTemplate;

    }

}
  